﻿using CommonControlLibrary;
using SQLQuery.SplashScreen;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Xml.Linq;

namespace SQLQuery
{
    class App
    {
        static IDesignWindow win;

        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        static void Main()
        {
            if (Skynet.CommonLibrary.SingleProcess.IsSingleProcess)
            {
                SplashScreenWindow sp = new SplashScreenWindow();
                sp.SetProgress(20, "正在加载控件……");
                sp.Show();
                AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
                if (System.IO.Directory.Exists("UpdateCopy"))
                {
                    System.IO.Directory.Delete("UpdateCopy", true);
                }
                //启动参数处理

                try
                {

                    if (IsSimpleControl())
                    {
                        Assembly ass = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "SimpleControlLibrary.dll");
                        Type type = ass.GetType("SimpleControlLibrary.DesignWindow");
                        win = Activator.CreateInstance(type) as IDesignWindow;
                    }
                    else
                    {
                        Assembly ass = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "DevControlLibrary.dll");
                        Type type = ass.GetType("DevControlLibrary.DesignWindow");
                        win = Activator.CreateInstance(type) as IDesignWindow;
                    }
                

                    if (win != null)
                    {
                        Application app = new Application();
                        IEnumerable<ResourceDictionary> resourceList;
                        win.CreateResourceDictionary(out resourceList);
                        foreach (ResourceDictionary dic in resourceList)
                        {
                            app.Resources.MergedDictionaries.Add(dic);
                        }
                        win.GetSplash = sp;
                        Window mainwindow = win.GetMainWindow();
                        //RibbonControlLibrary.RibbonMainWindow mainwindow = new RibbonControlLibrary.RibbonMainWindow();
                        app.ShutdownMode = ShutdownMode.OnMainWindowClose;

                        app.Run(mainwindow);

                    }
                }
                catch (Exception ex)
                {
                    sp.CloseSplash();
                    MessageBox.Show(ex.Message);
                    Application.Current.Shutdown(-1);
                }
                //
            }
            else
            {
                Application.Current.Shutdown(-1);
            }

        }

        private static bool IsSimpleControl()
        {
            string path = String.Format("{0}\\QueryConfig.xml", System.AppDomain.CurrentDomain.BaseDirectory);
            if (!File.Exists(path))
                return false;
            XElement element = XElement.Load(path);
            XElement xe = element.Elements().FirstOrDefault(o => o.Name.LocalName == "IsSimpleControl");
            if (xe == null)
            {
                return false;
            }
            bool isSimple = false;
            bool.TryParse(xe.Value, out isSimple);
            return isSimple;
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.IsTerminating)
            {
                if (MessageBox.Show("非常抱歉，由于系统的不稳定造成了崩溃,为您使用带来了不便，请多原谅！\r\n是否需要保存所有打开的文档？", "崩溃了", MessageBoxButton.OKCancel, MessageBoxImage.Error) == MessageBoxResult.OK)
                {
                    if (win != null)
                        win.SaveDocumentContent();
                }
                Environment.Exit(0);
            }

        }
    }
}
